#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m disk [OPTIONS] [ARGUMENTS]

Description: Manage disks and volumes on macOS using diskutil.

Options:
  --info DISK_NAME                                 Display disk information
  --list [DISK_NAME]                               List all disks or a specific disk if 'DISK' is provided (disk is optional)
  --filesystems                                    List available filesystems for formatting
  --ejectall                                       Eject all mountable Volumes
  --verify <disk|volme> DISK_NAME|VOLUME_NAME      Verify a volume or disk
  --repair <disk|volume> DISK_NAME|VOLUME_NAME     Repair a volume or disk
  --reformat VOLUME                                Reformat a volume
  --rename CURRENT_NAME NEW_NAME                   Rename a volume
  --format <disk|volume> FS_TYPE NEW_NAME DISK_NAME|VOLUME_NAME  Format a disk or volume with a specified filesystem type and name
__EOF__
}

disk_ls(){
    if [ -z "${1}" ]; then
        diskutil list
    else
        diskutil list "${1}"
    fi
}

disk_info(){
    [ -z "${1}" ] && help && exit 1
    diskutil info "${1}"
}

list_filesystems(){
    diskutil listFilesystems
}

ejectall(){
    osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'
}

verify(){
    case $1 in
        volume)
            [ -z "${2}" ] && help && exit 1
            diskutil verifyVolume "${2}"
            ;;
        disk)
            [ -z "${2}" ] && help && exit 1
            diskutil verifyDisk "${2}"
            ;;
        *)
            help
            ;;
    esac
}

repair(){
    case $1 in
        volume)
            [ -z "${2}" ] && help && exit 1
            diskutil repairVolume "${2}"
            ;;
        disk)
            [ -z "${2}" ] && help && exit 1
            diskutil repairDisk "${2}"
            ;;
        *)
            help
            ;;
    esac
}


format(){
    case $1 in
        volume)
            shift
            [ -z "$1" -o -z "$2" -o -z "$3" ] && help && exit 1
            diskutil eraseVolume "${1}" "${2}" "${3}"
            ;;
        disk)
            shift
            [ -z "$1" -o -z "$2" -o -z "$3" ] && help && exit 1
            diskutil eraseDiskk "${1}" "${2}" "${3}"
            ;;
        *)
            help
            ;;
    esac
}

reformat(){
    [ -z "$1" ] && help && exit 1
    diskutil reformat "${1}"
}

rename(){
    [ -z "$1" -o -z "$2" ] && help && exit 1
    diskutil rename "${1}" "${2}"

}


# Main script execution
option="${1}"
shift

case "${option}" in
    help)
        help
        ;;
    --list)
        disk_ls "$@"
        ;;
    --info)
        disk_info "$@"
        ;;
    --filesystems)
        list_filesystems
        ;;
    --ejectall)
        ejectall
        ;;
    --verify)
        verify "$@"
        ;;
    --repair)
        repair "$@"
        ;;
    --format)
        format "$@"
        ;;
    --reformat)
        reformat "$@"
        ;;
    --rename)
        rename "$@"
        ;;
    *)
        help && exit 1
        ;;
esac
